import User.people;
import com.alibaba.fastjson.JSON;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "Manager")
public class Manager extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        process(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        process(request,response);
    }

    protected void process(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
    {
        String token=request.getParameter("token");

        List<NameValuePair> formparams = new ArrayList<>();
        formparams.add(new BasicNameValuePair("token", token));
        formparams.add(new BasicNameValuePair("appid", "javaee"));
        formparams.add(new BasicNameValuePair("appsecret", "b3b52e43ccfd"));
        formparams.add(new BasicNameValuePair("userip", request.getLocalAddr()));
        String jsonString = QuickStart.post("https://cas.dgut.edu.cn/ssoapi/v2/checkToken", formparams);
        Return re= JSON.parseObject(jsonString,Return.class);

        List<NameValuePair> formpparams2 = new ArrayList<>();
        formpparams2.add(new BasicNameValuePair("openid", re.getOpenid()));
        formpparams2.add(new BasicNameValuePair("access_token", re.getAccess_token()));
        String jsonString2 = QuickStart.post("https://cas.dgut.edu.cn/oauth/getUserInfo", formpparams2);


        people p=JSON.parseObject(jsonString2,people.class);
        request.getSession().setAttribute("LOGIN",p);
        response.sendRedirect("/dgut_login");
    }
}
